import React from 'react';
import { Form, Input, Button, message } from 'antd';
import { connect } from 'umi';

const currControl = "Edu_Distributor";
class CourseEdit extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            datas: {},
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
    }
    onFinish = async (values: any) => {
        const { ParentData } = this.props;
        var act = "Abp/updatePassword";
        var payload = values.model;
        payload.id = ParentData.id
        this.props.dispatch({
            type: act,
            payload,
            control: currControl,
        }).then(() => {
            var resp = this.props.response;
            if (resp.success) {
                message.success('保存成功！');
            }
            else {
                message.error('保存错误，请联系管理员');
            }
        });
    };
    checkPsw = (rule: any, value: any, callback: any) => {
        if (value) {
            var datas = this.formRef.current.getFieldsValue();
            if (value == datas.model.oldPassword) {
                callback('新密码不能与原始密码相同');
            }
            else if (value != datas.model.confirmPsw && datas.model.confirmPsw) {
                callback('两次密码输入不一致');
            }
            else {
                callback();
            }
        }
        else { callback('两次密码输入不一致'); }
    }
    render() {
        const { loading } = this.state;
        return (
            <div>
                <Form ref={this.formRef} layout="vertical" onFinish={this.onFinish} >
                    <Form.Item
                        name={['model', 'oldPassword']}
                        label="旧密码"
                        rules={[{ required: true, message: "请输入旧密码" }]}>
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name={['model', 'confirmPsw']}
                        label="新密码"
                        rules={[{ required: true, message: "请输入新密码" }]}>
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name={['model', 'newPassword']}
                        label="确认新密码"
                        rules={[{ required: true,  validator: this.checkPsw }]}>
                        <Input />
                    </Form.Item>
                    <Form.Item>
                        <Button
                            type="primary"
                            htmlType="submit"
                            loading={loading}
                        >保存</Button>
                    </Form.Item>
                </Form>
            </div>
        )
    }
}
export default connect(({ Abp }: API.IConnectState) => ({
    response: Abp.response,
    currentUser: Abp.currentUser,
}))(CourseEdit);